//
//  PywRegisterMainViewController.m
//  PywSdk2.2_Test
//
//  Created by smallwave on 16/7/28.
//  Copyright © 2016年 yyft. All rights reserved.
//

#import "PywRegisterMainViewController.h"
#import "PywTextFieldView.h"
#import "UIButton+Pyw.h"
#import "UIImage+Pyw.h"
#import "PywMasonry.h"
#import "PywCommonManager.h"
#import "PywMBProgressHUD+Pyw.h"
#import "PywGetSmsCodeApi.h"
#import "PywRegisterValidCodeViewController.h"
#import "PywRegisterSuccessViewController.h"
#import "ApiClientConst.h"
#import "MutableDictionary.h"
#import "PywDisclaimerView.h"
#import "PywDisclaimerViewController.h"
#import "PywBuriedManager.h"

@interface PywRegisterMainViewController ()<PywDisclaimerViewDelegate>

@property (nonatomic, strong) PywTextFieldView  *phoneTextView; // 手机号码输入框
@property (nonatomic, strong) UIButton          *validCodeBtn;  // 获取验证码按钮
@property (nonatomic, strong) PywDisclaimerView *disclaimerView;

@property (nonatomic, copy) NSString            *smsType;       // 获取验证码场景类型
@property (nonatomic, strong) PywGetSmsCodeApi  *getSmsCodeApi; // 获取验证码请求

@end

@implementation PywRegisterMainViewController
KeyBoardHelper(90)


#pragma mark - Lift Cycle Method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"%s",__FUNCTION__);
    [PywBuriedManager manager].page_sn = @"7";
    [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"7" actionSn:@"1" typeSn:@"1"];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSLog(@"%s",__FUNCTION__);
    [PywBuriedManager manager].page_sn = @"";
//    [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"7" actionSn:@"2" typeSn:@"2"];
}
- (void)returnback
{
    [super returnback];
    [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"7" actionSn:@"7" typeSn:@"3"];
}
#pragma mark - UI Interface
- (void)setUp{
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"手机注册(1/3)";
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self addSubviewAndLayoutSubviews];
}
- (void)addSubviewAndLayoutSubviews{
    
    [self.view addSubview:self.phoneTextView];
    [self.view addSubview:self.validCodeBtn];
    [self.view addSubview:self.disclaimerView];
    
    __weak typeof(self) wSelf = self;
    [self.phoneTextView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.view.mas_top).offset(80);
        make.width.equalTo(wSelf.view.mas_width).multipliedBy(0.85);
        make.height.equalTo(@40);
    }];
    
    [self.validCodeBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.phoneTextView.mas_bottom).offset(40);
        make.width.height.equalTo(wSelf.phoneTextView);
    }];
    
    [self.disclaimerView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.validCodeBtn.mas_bottom).offset(10);
        make.width.height.equalTo(wSelf.phoneTextView);
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

#pragma mark - Action
- (void)getValidCodeCilck{
    [self.view endEditing:YES];

    // 判断输入手机号 是否合法
    NSString *phoneNum = [self.phoneTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (phoneNum.length <= 0) {
        
        [PywMBProgressHUD showError:@"请填写手机号" toView:self.view];
    } else if (![[PywCommonManager manager] validatePhone:self.phoneTextView.text]){
        
        [PywMBProgressHUD showError:@"手机号码不合法！" toView:self.view];
    } else {
        
        [[PywBuriedManager manager] saveUserActionDataWithPageSn:@"7" actionSn:@"10" typeSn:@"3"];
        [self.getSmsCodeApi requestGetSmsCodeWithParams:[self requestGetValidCodeParams] success:^(NSString *status, id anyObjct) {
            
            NSInteger is_reg = [status integerValue];
            switch (is_reg) {
                case 0:// 验证码正确，手机号未注册【跳转到“快速注册（2/3）”】
                {
                    PywRegisterValidCodeViewController *validCodeVC = [[PywRegisterValidCodeViewController alloc] initWithPhoneNumber:self.phoneTextView.text smsType:self.smsType];
                    [self.navigationController pushViewController:validCodeVC animated:YES];
                }
                    break;
                case 1:// 验证码正确，手机号已注册【跳转到“温馨提示”】
                {
                    PywRegisterSuccessViewController *successVC = [[PywRegisterSuccessViewController alloc] initWithPhoneNumber:self.phoneTextView.text];
                    [self.navigationController pushViewController:successVC animated:YES];
                }
                    break;
            }
        } fail:^(NSError *error) {
            
        }];
    }
}

/**
 *  获取短信验证码  传入参数
 *
 *  @return <#return value description#>
 */
- (MutableDictionary *)requestGetValidCodeParams{
    NSString *phoneNumber = [self.phoneTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    self.smsType = [NSString stringWithFormat:@"%d",SMSCODETYPE_REGISTER];
    
    MutableDictionary *params = [[MutableDictionary alloc] init];
    [params setObject:phoneNumber forKey:@"phone"];
    [params setObject:self.smsType forKey:@"smstype"];
    return params;
}

#pragma mark - PywDisclaimerViewDelegate
- (void)didButton:(UIButton *)button index:(NSInteger)index
{
    if (index == ButtonTag_Disclaimer) { // 免责声明

        PywDisclaimerViewController *disclaimerVC = [[PywDisclaimerViewController alloc] init];
        [self presentViewController:disclaimerVC animated:YES completion:NULL];
    }else{
        
        if (!button.selected) {
            self.validCodeBtn.enabled = YES;
            [self.validCodeBtn setBackgroundColor:UIColorFromRGB(0xFFBC00)];
        }else{
            self.validCodeBtn.enabled = NO;
            [self.validCodeBtn setBackgroundColor:[UIColor lightGrayColor]];
        }
    }
}

#pragma mark - lazy
- (PywTextFieldView *)phoneTextView{
    if (!_phoneTextView) {
        _phoneTextView = [PywTextFieldView phoneTextFieldWithLeftImg:@"icon_phone_normal"
                                                     TextPlaceholder:@"请输入您的手机号码"];
        [_phoneTextView setAccessibilityId:@"phonereg_phone"];
    }
    return _phoneTextView;
}
- (PywDisclaimerView *)disclaimerView
{
    if (!_disclaimerView) {
        _disclaimerView = [[PywDisclaimerView alloc] initWithDelegate:self];
    }
    return _disclaimerView;
}
- (UIButton *)validCodeBtn{
    if (!_validCodeBtn) {
        _validCodeBtn = [UIButton buttonWithBackgroundColor:RGB(252, 177, 14, 1)
                                               cornerRadius:20
                                                 titleColor:[UIColor whiteColor]
                                         titleLabelFontSize:18
                                                      title:@"获取验证码"];
        
        [_validCodeBtn addTarget:self action:@selector(getValidCodeCilck) forControlEvents:UIControlEventTouchUpInside];
        [_validCodeBtn setAccessibilityIdentifier:@"phonereg_getcode"];
    }
    return _validCodeBtn;
}
- (PywGetSmsCodeApi *)getSmsCodeApi{
    if (!_getSmsCodeApi) {
        _getSmsCodeApi = [PywGetSmsCodeApi manager];
    }
    return _getSmsCodeApi;
}

@end
